package com.example.serialportdemo.view;

import static android.opengl.ETC1.getHeight;
import static android.opengl.ETC1.getWidth;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * @auth: njb
 * @date: 2023/12/31 13:10
 * @desc: 自定义温度view
 */
public class TemperatureView extends View {
    private int temperature = 25; // 默认温度
    private float minTemperature = 16; // 最小温度
    private float maxTemperature = 30; // 最大温度
    private float tickMarkLength = 20; // 刻度线长度
    public TemperatureView(Context context) {
        super(context);
        init();
    }

    public TemperatureView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TemperatureView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        // 初始化操作，例如设置颜色、字体等
    }

    public void setTemperature(int temperature) {
        this.temperature = temperature;
        invalidate(); // 重新绘制视图
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 绘制温度视图
        float width = getWidth();
        float height = getHeight();

        // 绘制刻度线
        Paint tickMarkPaint = new Paint();
        tickMarkPaint.setColor(Color.GRAY);
        tickMarkPaint.setStrokeWidth(2);

        float centerX = width / 2;
        float centerY = height / 2;

        float radius = Math.min(width, height) / 2;
        float startAngle = 150;
        float sweepAngle = 240;

        RectF arcRect = new RectF(centerX - radius, centerY - radius, centerX + radius, centerY + radius);

        canvas.drawArc(arcRect, startAngle, sweepAngle, false, tickMarkPaint);

        float angleUnit = sweepAngle / (maxTemperature - minTemperature);

        Paint temperaturePaint = new Paint();
        temperaturePaint.setColor(Color.RED);
        temperaturePaint.setTextSize(30);
        temperaturePaint.setTextAlign(Paint.Align.CENTER);

        for (float i = minTemperature; i <= maxTemperature; i++) {
            float angle = startAngle + (i - minTemperature) * angleUnit;
            float tickX = centerX + (radius - tickMarkLength) * (float) Math.cos(Math.toRadians(angle));
            float tickY = centerY + (radius - tickMarkLength) * (float) Math.sin(Math.toRadians(angle));

            canvas.drawLine(centerX, centerY, tickX, tickY, tickMarkPaint);
            canvas.drawText(String.valueOf((int) i), tickX, tickY + 12, temperaturePaint);
        }

        // 绘制当前温度
        float temperatureAngle = startAngle + (temperature - minTemperature) * angleUnit;
        float temperatureX = centerX + (radius - tickMarkLength * 2) * (float) Math.cos(Math.toRadians(temperatureAngle));
        float temperatureY = centerY + (radius - tickMarkLength * 2) * (float) Math.sin(Math.toRadians(temperatureAngle));

        Paint currentTemperaturePaint = new Paint();
        currentTemperaturePaint.setColor(Color.RED);
        currentTemperaturePaint.setTextSize(40);
        currentTemperaturePaint.setTextAlign(Paint.Align.CENTER);

        canvas.drawText(String.valueOf(temperature), temperatureX, temperatureY + 20, currentTemperaturePaint);
    }
}
